// pages/search/index.js
const{
  pushProjects,pushMaintains,pushTasks
} =require('../../api/push')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPopupOptin: false,
    option: [],
    is_need_time: true,
    ruleForm: {
      all_project:false,
      project_id: '',
      maintain_id: '',
      do_uid: 0,
      radio_type: 1,
      is_desc: 0,
      do_task_type: 0,
      finish_date: '',
      maintain_name_id: 0,
      name: ''
    },
    is_desc: '0',
    radio_type: '1',
    project_list: [],
    maintain_list: [],
    showTime: false,
    role_list: [
      {text:"区域经理",value:10},
      {text:"项目经理",value:11},
      {text:"项目主管",value:12},
      {text:"领班",value:14}
    ],
    typeOption: [
      {text:"浇灌",value:1,children:[{text:"草坪浇灌",value:101},{text:"绿篱、地被浇灌",value:102},{text:"乔灌木浇灌",value:101}]},
      {text:"修剪",value:1,children:[{text:"乔木修剪",value:101},{text:"球类修剪",value:102},{text:"绿篱色块修剪",value:101}]},
      {text:"施肥",value:1,children:[{text:"乔灌木施肥",value:101},{text:"绿篱地被施肥",value:102},{text:"草坪施肥",value:101}]},
      {text:"苗木补植",value:1,children:[{text:"草坪浇灌",value:101},{text:"绿篱、地被浇灌",value:102},{text:"乔灌木浇灌",value:101}]},
      {text:"草坪复壮",value:1,children:[{text:"草坪浇灌",value:101},{text:"绿篱、地被浇灌",value:102},{text:"乔灌木浇灌",value:101}]},
      {text:"风控管理",value:1,children:[{text:"草坪浇灌",value:101},{text:"绿篱、地被浇灌",value:102},{text:"乔灌木浇灌",value:101}]},
      {text:"绿地整理",value:1,children:[{text:"草坪浇灌",value:101},{text:"绿篱、地被浇灌",value:102},{text:"乔灌木浇灌",value:101}]}
    ],
    selName:''

  },
  onDisplayTime() {
    this.setData({ showTime: true });
  },
  onCloseTime() {
    this.setData({ showTime: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirmTime(event) {
    this.setData({
      showTime: false,
      finish_date: this.formatDate(event.detail),
    });
    this.data.ruleForm['finish_date'] = this.formatDate(event.detail)
  },
  selrelation(e) {
    let showPopup = true
    switch (e.target.dataset.name) {
      case 'project_select':
        this.data.option = []
        this.data.option = this.data.relationOption
        this.data.option.push({text:"所有项目",value:99999})
        break
      case 'maintain_select':
        this.data.option = []
        this.data.maintain_list.forEach((item) => {
          
          if (item.parent_id == 0) {
            this.data.option.push(item)
          }
          
        })
        this.data.ruleForm['maintain_name_id'] = 0
        this.data.ruleForm['maintain_name_select'] = ''
        //this.data.option = this.data.maintain_list
        break
      case 'maintain_name_select':
        this.data.option = []
        this.data.maintain_list.forEach((item) => {
          
          if (item.parent_id == this.data.ruleForm.maintain_id) {
            this.data.option.push(item)
          }
        })
        break
      case 'user_select': // 行业
        this.data.option = []
        if (this.data.ruleForm.project_id == 99999) {
          this.data.ruleForm['all_project'] = true
          this.data.option = this.data.role_list
          break
        }
        this.data.project_list.forEach((item) => {
          if (this.data.ruleForm.project_id == item.id) {
            this.data.option = item.user_list.map(function(kitem){
              return { text: kitem.name, value: kitem.id };
            })
          }
          
        });
        break
      default:
    }
    if (this.data.option.length < 1) {
      showPopup =  false
    }
    this.data.selName = e.target.dataset.name
    this.setData({
      showPopup: showPopup,
      option: this.data.option,
      ruleForm: this.data.ruleForm
    })
  },
  onCancel() {
    this.data.option = []
    this.setData({
      showPopup: false,
      option:[]
    })
  },
  onChangeRadio(event) {
    this.data.ruleForm['radio_type'] = event.detail
    this.setData({
      radio_type: event.detail,
    });
    if (event.detail != '1') {
      this.setData({
        is_need_time: false
      });
    } else {
      this.setData({
        is_need_time: true
      });
    }
    console.log(this.data.ruleForm)
  },
  onChangeIsdesc(event) {
    this.setData({
      is_desc: event.detail,
    });
    this.data.ruleForm['is_desc'] = event.detail
  },
  onChangeDoTaskType(event) {
    this.setData({
      do_task_type: event.detail,
    });
    this.data.ruleForm['do_task_type'] = event.detail
  },
  onConfirm(event) {
    console.log(this.data.selName)
    console.log(event.detail)
    let arr = this.data.selName.split('.')
    let value =
      typeof event.detail.value == 'string'
        ? event.detail.value
        : event.detail.value.text
    if (arr.length > 1) {
      this.data.ruleForm[arr[0]][arr[1]] = value
    } else {
      this.data.ruleForm[this.data.selName] = value
      if (this.data.selName == 'project_select') {
        this.data.ruleForm['project_id'] = event.detail.value.value
        this.data.ruleForm['user_select'] = ''
      }else if (this.data.selName == 'maintain_select') {
        this.data.ruleForm['maintain_id'] = event.detail.value.value
      }else if (this.data.selName == 'maintain_name_select') {
        this.data.ruleForm['maintain_name_id'] = event.detail.value.value
      } else if (this.data.selName == 'user_select') {
        this.data.ruleForm['do_uid'] = event.detail.value.value
      } else {
        this.data.ruleForm[this.data.selName+'_real'] = event.detail.value.value
      }
    }
    let ruleForm = this.data.ruleForm
    let showPopup = (this.data.showPopup = false)
    this.setData({ ruleForm, showPopup })
  },
  submit() {
    console.log(this.data.ruleForm)
    if (this.data.ruleForm.maintain_name_id && this.data.ruleForm.maintain_name_id != 0) {
      this.data.ruleForm.maintain_id = this.data.ruleForm.maintain_name_id
    }
    if (this.data.ruleForm.project_id && this.data.ruleForm.do_uid) {
      pushTasks(this.data.ruleForm).then(res=>{
        wx.navigateTo({
          url: '/pages/myPushTask/index',
        })
      })
    } else {
      wx.showModal({
        title: "温馨提示", // 提示的标题
        content: "请选择必填项", // 提示的内容
        showCancel: false, // 是否显示取消按钮，默认true
        confirmText: "确定", // 确认按钮的文字，最多4个字符
        confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
        success: function (res) {
          return
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '数据加载中',
    })

    pushProjects({}).then(res=>{
      console.log(res)
      this.setData({
        project_list: res.data,
        relationOption:res.data.map(item => {
          return { text: item.project_name, value: item.id };
        })
      })
      pushMaintains({}).then(res=>{
        this.setData({
          maintain_list:res.data.map(item => {
            return { text: item.name, value: item.id , parent_id: item.parent_id };
          })
        })
        console.log(res.data)

        wx.hideLoading({
          complete: (res) => {},
        })
        wx.hideNavigationBarLoading(); //完成停止加载图标
        wx.stopPullDownRefresh();
      })
      
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})